home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
ainet
/
t2runtim.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-11
|
13KB
|
356 lines
/* ----------------------------------------------------------------------- *
* *
* (C) Copyright 1996 by: aiNet *
* Trubarjeva 42, SI-3000 Celje *
* Europe, Slovenia *
* All Rights Reserved *
* *
* Subject: C code for single vector prediction. *
* File: t2runtim - The XOR problem with dynamic model creation *
* *
* ----------------------------------------------------------------------- */
/*--------------------------------------------------------------------------
Here it will be shown how we can solve the XOR problem using
aiNet C functions. The problem is the same as in DLLTST02.
The XOR problem:
================
Number of model vectors: 4
Number of variables: 3
Number of input variables: 3
Any discrete variables: NONE
Model vectors: Inp,Inp,Out
row 1: 1, 1, 0
row 2: 1, 0, 1
row 3: 0, 1, 1
row 4: 0, 0, 0
Test vectors (vectors which will be used in prediction) together with
penalty coefficient and penalty method.
Prediction vectors: Inp Inp Out
prd 1: 0.9 0.1 ??
prd 2: 0.1 0.9 ??
prd 3: 0.2 0.2 ??
prd 4: 0.7 0.7 ??
Penalty coeffcient: 1.5
Penalty methods: NEAREST
NOTE: Selected penalty coefficients are in no case optimal.
They were selected randomly, to perform a few tests.
The test results were compared with the results calculated by
the main aiNet 1.14 application.
--------------------------------------------------------------------------
Results (rounded at fourth decimal):
--------------------------------------------------------------------------
Penalty cefficient: 1.5
Penalty method: NEAREST N.
(RESULT)
Prediction vectors: Inp Inp ( Out )
prd 1: 0.9 0.1 (0.9989)
prd 2: 0.1 0.9 (0.9989)
prd 3: 0.2 0.2 (0.1054)
prd 4: 0.7 0.7 (0.3449)
-------------------------------------------------------------------------*/
/*
* This file assumes that ainetxx.dll will be loaded at run time,
* which is default option and no flags need to be defined.
* ainetxx.lib must NOT be included in the linking process.
*/
#include "ainetdll.h"
#include <stdio.h>
#include <stdlib.h>
/*
* Path and the filename of dll which will be loaded.
*/
#if defined(__WIN32__)
const char ainetDll[] = "ainet32.dll";
#else
const char ainetDll[] = "ainet16.dll";
#endif
/*
* Pointers to ainet dll functions. They are made global - all functions
* can use them.
*/
t_aiRegistration aiRegistration;
t_aiGetVersion aiGetVersion;
t_aiCreateModel aiCreateModel;
t_aiCreateModelFromCSVFile aiCreateModelFromCSVFile;
t_aiDeleteModel aiDeleteModel;
t_aiNormalize aiNormalize;
t_aiDenormalize aiDenormalize;
t_aiPrediction aiPrediction;
t_aiGetNumberOfVariables aiGetNumberOfVariables;
t_aiGetNumberOfModelVectors aiGetNumberOfModelVectors;
t_aiGetNumberOfInputVariables aiGetNumberOfInputVariables;
t_aiSetDiscreteFlag aiSetDiscreteFlag;
t_aiGetDiscreteFlag aiGetDiscreteFlag;
t_aiSetVariable aiSetVariable;
t_aiGetVariable aiGetVariable;
t_aiGetVariableVB aiGetVariableVB;
t_aiGetCSVFileModelSize aiGetCSVFileModelSize;
// New in version 1.24
t_aiSetCapacity aiSetCapacity;
t_aiGetCapacity aiGetCapacity;
t_aiGetFreeEntries aiGetFreeEntries;
t_aiInsertModelVector aiInsertModelVector;
t_aiOverwriteModelVector aiOverwriteModelVector;
t_aiAppendModelVector aiAppendModelVector;
t_aiDeleteModelVector aiDeleteModelVector;
t_aiPredictionEx aiPredictionEx;
t_aiExcludeModelVector aiExcludeModelVector;
t_aiExcludeModelVectorRange aiExcludeModelVectorRange;
t_aiIsModelVectorExcluded aiIsModelVectorExcluded;
t_aiSaveCSVFile aiSaveCSVFile;
/*
* ainet32.dll module variable.
*/
HINSTANCE hLib;
/*
* The load_aiNetLibrary() function is implemented below.
* This function will load ainet32.dll and define pointers to
* ainet functions.
*/
int load_aiNetLibrary(void);
/*
*
*/
void main()
{
/*
* Working variables
*/
int i;
int version;
/*
* vectors to be predicted
*/
float predict[4][3] = { { 0.9,0.1, 999 },
{ 0.1,0.9, 999 },
{ 0.2,0.2, 999 },
{ 0.7,0.7, 999 } };
/*
* Model variable
*/
aiModel* model;
/*
* Load DLL
*/
if( !load_aiNetLibrary() ) {
exit(EXIT_FAILURE);
}
/*
* Title
*/
version = aiGetVersion();
printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1996",
version/100, version%100 );
printf( "\n---------------------------------------------------\n" );
/*
* Register DLL
*/
aiRegistration( "Your registration name", "Your code" );
/*
* Allocate the model variable and necessary memory
*/
model = aiCreateModel(4, /* 4 model vectors */
3, /* 3 variables */
2); /* 2 input variables */
if(!model) {
printf( "\nError: Something went wrong during model creation!" );
exit(EXIT_FAILURE);
}
/*
* Loading data into the model using aiSetVariable function
*/
aiSetVariable(model,1,1, 1.0); /* first model vector */
aiSetVariable(model,1,2, 1.0); /* 1 xor 1 = 0 */
aiSetVariable(model,1,3, 0.0);
aiSetVariable(model,2,1, 1.0); /* second model vector */
aiSetVariable(model,2,2, 0.0); /* 1 xor 0 = 1 */
aiSetVariable(model,2,3, 1.0);
aiSetVariable(model,3,1, 0.0); /* third model vector */
aiSetVariable(model,3,2, 1.0); /* 0 xor 1 = 1 */
aiSetVariable(model,3,3, 1.0);
aiSetVariable(model,4,1, 0.0); /* fourth model vector */
aiSetVariable(model,4,2, 0.0); /* 0 xor 0 = 0 */
aiSetVariable(model,4,3, 0.0);
/*
* Output the model
*/
printf( "\n Model name: aiNet DLL test 2 (aiCreateModel)" );
printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(model));
printf( "\n Number of variables: %i", aiGetNumberOfVariables(model));
printf( "\n Variable names: A, B, A xor B" );
printf( "\n Discrete flag: %i, %i, %i",
aiGetDiscreteFlag(model,1),
aiGetDiscreteFlag(model,2),
aiGetDiscreteFlag(model,3) );
for( i=1; i<=aiGetNumberOfModelVectors(model); i++ ) {
printf( "\n\t\t\t %3.1lf, %3.1lf, %3.1lf",
aiGetVariable(model, i,1),
aiGetVariable(model, i,2),
aiGetVariable(model, i,3) );
}
/*
* Normalize the model
*/
aiNormalize(model, NORMALIZE_REGULAR);
/*
* Prediction
* This test has nearest neighbour penalty coefficient 1.50
*/
printf( "\n\n Penalty coefficient: 1.50" );
printf( "\n Penalty method: NEAREST N." );
printf( "\n\t A(inp), B(inp), A xor B(out)" );
for ( i=0; i<4; i++ ) {
aiPrediction(model, predict[i], 1.50, PENALTY_NEAREST);
printf( "\n\t%7.4lf, %7.4lf, %7.4lf",
predict[i][0],predict[i][1],predict[i][2] );
}
/*
* Denormalize the model (in this case it is not necessary)
*/
aiDenormalize(model);
/*
* Free alocated memory
*/
aiDeleteModel(model);
FreeLibrary(hLib);
printf( "\n\nEnd." );
exit(EXIT_SUCCESS);
}
int load_aiNetLibrary()
{
/*
* Load the Dynamic Link Library AINET32.DLL
*/
hLib = LoadLibrary(ainetDll);
if((unsigned)hLib<=HINSTANCE_ERROR){
char bfr[40];
wsprintf(bfr, "Failure loading library: %s", ainetDll);
MessageBox(NULL, bfr, "Error", MB_OK|MB_APPLMODAL);
return 0;
}
/*
*